/* 
 * File:   OpenCVWrapper.h
 * Author: alrojas
 *
 * Created on 14 de junio de 2014, 11:20
 */

#ifndef OPENCVWRAPPER_H
#define	OPENCVWRAPPER_H

#include <QImage>
#include <QPixmap>
#include "opencv2/imgproc/types_c.h"
#include "opencv2/imgproc/imgproc.hpp"

namespace OpenCVWrapper{
    
    inline QImage MatToQImage(const cv::Mat & inMat){
        
        switch (inMat.type()){
            
        // 8-bit, 4 channel
        case CV_8UC4:{
            QImage image(inMat.data,inMat.cols,inMat.rows,inMat.step,QImage::Format_RGB32);
 
            return image;
         }
 
         // 8-bit, 3 channel
        case CV_8UC3:{            
            QImage image(inMat.data,inMat.cols,inMat.rows,inMat.step,QImage::Format_RGB888);
 
            return image.rgbSwapped();
        }
 
         // 8-bit, 1 channel
        case CV_8UC1:{

            static QVector < QRgb > sColorTable;
 
            // only create our color table once
            if (sColorTable.isEmpty()){
                for (int i = 0; i < 256; ++i)
                    sColorTable.push_back(qRgb(i,i,i));
            }
 
            QImage image(inMat.data,inMat.cols,inMat.rows,inMat.step,QImage::Format_Indexed8);
 
            image.setColorTable(sColorTable);
 
            return image;
        }
 
        default:
            //qWarning() << "OpenCVWrapper::MatToQImage() - cv::Mat image type not handled in switch:" << inMat.type();
            break;
        }
 
        return QImage();
    }
 
    inline QPixmap MatToQPixmap(const cv::Mat & inMat){
        return QPixmap::fromImage(MatToQImage(inMat));
    }
}

#endif	/* OPENCVWRAPPER_H */

